#include "ymaker-nrf24l01.h"
#include "i2cReg.h"


bool nrf24l01Class::check()
{
    if(readReg8(I2C_ADD,0x0a)==0x55)
    {
        return true;
    }
    else return false;
}
void nrf24l01Class::setTxAdd(uint8_t *add)
{
    writeRegNoLength(I2C_ADD,REG_TX_ADD,5,add);
}
void nrf24l01Class::setRxAdd(uint8_t *add)
{
    writeRegNoLength(I2C_ADD,REG_RX_ADD,5,add);
}
void nrf24l01Class::writeData(uint8_t *add,uint8_t len,uint8_t *dat)
{
    uint8_t allLen=5+1+len;
    uint8_t buf[allLen];

    memcpy(buf,add,5);
    buf[5]=len;
    memcpy(buf+6,dat,len);
    writeRegNoLength(I2C_ADD,REG_TX_DATA,allLen,buf);
}
void nrf24l01Class::writeData(uint8_t *add,uint8_t dat)
{
    writeData(add,1,&dat);    
}
bool nrf24l01Class::readRxState()
{
    if(readReg8(I2C_ADD,REG_RX_STATE))return true;
    else return false;
}
void nrf24l01Class::readRxData(uint8_t *buf)
{
    readRegBuf(I2C_ADD,REG_RX_DATA,4,buf);
}
uint8_t nrf24l01Class::readRxData()
{
    uint8_t buf[4];
    readRxData(buf);
    return buf[0];
}
bool nrf24l01Class::readData(uint8_t *buf)
{
    if(readRxState()==false)return false;
    readRxData(buf);
    return true;
}
bool nrf24l01Class::writeReadData(uint8_t *add,uint8_t *tbuf,uint8_t *rbuf)
{
    uint8_t i;
    uint8_t sta;
    writeData(add,4,tbuf);
    do
    {      
      delay(2);
      i+=1;; 
      sta=readData(rbuf); 
    }
    while((sta==false)&&(i<10));
    
    if(sta==true)
    {
      delay(5);
      return true;  
    }
    else return false;
    
}
